package com.yangguang.huahui.ui.activitys;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.hyphenate.easeui.widget.EaseContactList;
import com.hyphenate.easeui.widget.EaseSidebar;
import com.yangguang.huahui.R;
import com.yangguang.huahui.entity.Contact;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.HttpResult;
import com.yangguang.huahui.mvp.model.NewsModel;
import com.yangguang.huahui.mvp.presenter.FriendsPresenter;
import com.yangguang.huahui.mvp.view.ContactView;
import com.yangguang.huahui.rx.RxManager;
import com.yangguang.huahui.rx.RxSubscriber;
import com.yangguang.huahui.ui.adapters.ContactAdapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FriendsActivity extends BaseMvpActivity<ContactView, FriendsPresenter> implements ContactView {
    @BindView(R.id.tv_title)
    TextView tv_title;
    String keywords = "";
    @BindView(R.id.mListView)
    ListView listView;
    @BindView(R.id.sidebar)
    EaseSidebar sidebar;
    private ContactAdapter adapter;
    List<Contact.ListBean> datas = new ArrayList<>();
    @Override
    protected int initLayoutId() {
        return R.layout.activity_friends;
    }

    @OnClick({R.id.iv_back})
    void onClick(View v){
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Override
    protected void initView() {
        tv_title.setText(getString(R.string.my_contacts));
        sidebar.setListView(listView);
        adapter = new ContactAdapter(mContext, datas, R.layout.contact_list_item);
        listView.setAdapter(adapter);

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(Contact contact) {
        getContactList(contact);
        datas = contact.getList();
        adapter.setNewData(datas);
    }

    private void getContactList(Contact contact) {
        // sorting
        Collections.sort(contact.getList(), new Comparator<Contact.ListBean>() {

            @Override
            public int compare(Contact.ListBean lhs, Contact.ListBean rhs) {
                if(lhs.getInitialLetter().equals(rhs.getInitialLetter())){
                    return lhs.getNickname().compareTo(rhs.getNickname());
                }else{
                    if("#".equals(lhs.getInitialLetter())){
                        return 1;
                    }else if("#".equals(rhs.getInitialLetter())){
                        return -1;
                    }
                    return lhs.getInitialLetter().compareTo(rhs.getInitialLetter());
                }

            }
        });
    }

    @Override
    public void onError() {

    }

    @Override
    protected FriendsPresenter initPresenter() {
        return new FriendsPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getFriends(DataInfo.TOKEN, keywords);
    }

    public void filter(CharSequence str) {
        adapter.getFilter().filter(str);
    }
}
